package com.bidevalution.business.job;

import com.bidevalution.business.factory.StepJobJdbcFactory;
import com.bidevalution.business.service.JdbcTemplateService;
import com.bidevalution.dao.entity.BeComputerResult;
import com.bidevalution.dao.entity.BeSectionInfo;
import com.bidevalution.dao.entity.BeSectionStepJob;
import com.qtp.core.util.DateUtil;
import com.qtp.core.util.IdsUtil;
import com.qtp.core.util.OConvertUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSourceUtils;

import java.util.*;

/**
 * @Description 标化工地增加费评审
 * @Date        2020-04-13 17:33
 * @Author      lhl
 */
public class StandardChemicalLandIncreaseFeeJob implements StepJobJdbcFactory {

    private JdbcTemplateService jdbcTemplateService;

    @Override
    public void setJdbcTemplateService(JdbcTemplateService jdbcTemplateService) {
        this.jdbcTemplateService = jdbcTemplateService;
    }

    @Override
    public Boolean execute(BeSectionStepJob beSectionStepJob, String submitCode, String expertId,String layouCode) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("sectionStepId", beSectionStepJob.getSectionStepId());
        paramMap.put("submitCode", submitCode);
        paramMap.put("sectionId", beSectionStepJob.getSectionId());
        RowMapper<BeSectionInfo> rm = BeanPropertyRowMapper.newInstance(BeSectionInfo.class);
        BeSectionInfo beSectionInfo = jdbcTemplateService.selectForObject(" select\n" +
                "    seq_id, section_stage, section_code, section_name, bid_type, budget_money, bid_open_time, \n" +
                "    tenderee, agent_org_code, agent_name, bid_open_address, submit_time, pt_section_id, \n" +
                "    section_area, eval_method, td_projectId, create_time, update_time" +
                "    from be_section_info\n" +
                "    where seq_id=:sectionId ", paramMap, rm);
        paramMap.put("tdProjectId", beSectionInfo.getTdProjectid());
        List<Map<String, Object>> enterList = jdbcTemplateService.selectForList("SELECT \n" +
                "COUNT(IF(z1.otno_Total = t1.otno_Total,1,NULL)) yz,\n" +
                "COUNT(IF(z1.otno_Total = t1.otno_Total,NULL,1)) byz,\n" +
                "t1.enterprise_id\n" +
                "FROM td_othernode_bid t1\n" +
                "LEFT JOIN td_othernode z1 ON  z1.otno_ProjectID = t1.otno_ProjectID  AND z1.otno_OtherNodeID = t1.otno_OtherNodeID \n" +
                "LEFT JOIN td_ManageNode c ON t1.otno_ProjectID=c.mano_ProjectID  AND t1.otno_ManageNodeID=c.mano_ManageNodeID\n" +
                "WHERE t1.otno_ProjectID = :tdProjectId AND t1.otno_Type = '9'  AND t1.enterprise_id IS NOT NULL AND t1.enterprise_id != ''\n" +
                "GROUP BY t1.enterprise_id",paramMap);

        //保存计算结果
        saveResult(beSectionStepJob.getSectionId(), beSectionStepJob.getSectionStepId(), enterList);
        return true;
    }

    /**
     * 保存计算结果
     * @param sectionId
     * @param sectionStepId
     * @param enterList
     * @throws Exception
     */
    private void saveResult(String sectionId, String sectionStepId, List<Map<String, Object>> enterList) throws Exception {
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("sectionStepId", sectionStepId);
        paramMap.put("sectionId", sectionId);
        List<BeComputerResult> beComputerResultList = new ArrayList<>();
        BeComputerResult beComputerResult = null;
        for(Map<String, Object> map: enterList) {
            beComputerResult = new BeComputerResult();
            beComputerResult.setSeqId(IdsUtil.uuid());
            beComputerResult.setSectionId(sectionId);
            beComputerResult.setSectionStepId(sectionStepId);
            beComputerResult.setC1(OConvertUtils.getString(map.get("yz")));
            beComputerResult.setC2(OConvertUtils.getString(map.get("byz")));
            beComputerResult.setEnterpriseId(OConvertUtils.getString(map.get("enterprise_id")));
            beComputerResult.setCreateTime(DateUtil.now());
            //不符合数=0,代表通过
            if (OConvertUtils.getInt(map.get("byz")) == 0) {
                beComputerResult.setIsPassed("1");
            } else {
                beComputerResult.setIsPassed("0");
            }
            beComputerResultList.add(beComputerResult);
        }
        //删除已存在的结果，防止重复存入
        jdbcTemplateService.update(" DELETE FROM be_computer_result WHERE  section_step_id = :sectionStepId and section_id=:sectionId", paramMap);
        //批量转数组,插入数据
        SqlParameterSource[] beanSources = SqlParameterSourceUtils.createBatch(beComputerResultList);
        jdbcTemplateService.bathInsert("INSERT INTO be_computer_result (\n" +
                "  seq_id,\n" +
                "  section_step_id,\n" +
                "  section_id,\n" +
                "  enterprise_id,\n" +
                "  c1,\n" +
                "  c2,\n" +
                "  create_time,\n" +
                "  is_passed\n" +
                ") VALUES (\n" +
                "    :seqId,\n" +
                "    :sectionStepId,\n" +
                "    :sectionId,\n" +
                "    :enterpriseId,\n" +
                "    :c1,\n" +
                "    :c2,\n" +
                "    :createTime,\n" +
                "    :isPassed\n" +
                "  )",beanSources);
    }

}
